#include "Material.h"


namespace SEngine
{


    Material::Material(const Material & that) 
    {   
        this->m_pso = that.m_pso;
        this->m_shader = that.m_shader;
        this->m_uniformDatas = that.m_uniformDatas;
    }

    void Material::Remove(const std::string &name)
    {
        if (m_uniformDatas.find(name) != m_uniformDatas.end()) {
            m_uniformDatas.erase(name);
        }
    }

    void Material::Bind()
    {
        if (!m_shader) {
            return;
        }
        m_shader.Value().Use();
        m_shader.Value().ForeachUniforms([this](const std::string & name, auto & type) {
            auto data = this->Get<std::decay_t<decltype(type)>>(name);
            if (data) {
                m_shader.Value().SetUniform(name, data.value());
            } else {
                m_shader.Value().SetUniform(name, type);
            }
        });
    }

    void Material::Unbind()
    {
        // if (m_shader.expired()) {
        //     return;
        // }
        // ResValue(m_shader).Use();
        // ResValue(m_shader).ForeachUniforms([this](const std::string & name, auto & type) {
        //     auto data = this->Get<std::decay_t<decltype(type)>>(name);
        //     if (data) {
        //         if constexpr (std::is_same_v<std::decay_t<decltype(type)>, Shader::Sampler>) {
        //             data->texture->get().Unbind();
        //             ResValue(m_shader).SetUniform(name, Shader::Sampler());
        //         }
        //     }
        // });
    }

} // namespace SEngine

